//
//  NSString+JwCode.m
//  JwPack
//
//  Created by 陈警卫 on 2024-07-16.
//

#import "NSString+JwCode.h"
#import "NSData+JwCate.h"

@implementation NSString (JwCode)

/// 转data
- (NSData *)jw_data {
    return [self dataUsingEncoding:NSUTF8StringEncoding];
}

/// 转字典
- (NSDictionary *)jw_json {
    NSError *error = nil;
    id value = [NSJSONSerialization JSONObjectWithData:[self jw_data] options:kNilOptions error:&error];
    if (error) {
        NSLog(@"jw_json error:%@", error);
    }
    if (![value isKindOfClass:[NSDictionary class]]) {
        NSLog(@"jw_json 警告: 返回的对象不是字典");
    }
    return value;
}

/// md5
- (NSString *)jw_md5String {
    return [[self jw_data] jw_md5String];
}

/// URL编码
- (NSString *)jw_urlEncode {
    NSString *string = self;
    if (![string isKindOfClass:[NSString class]]){
        return nil;
    }
    string = [string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    return string;
}

/// URL编码
- (NSURL *)jw_urlEncodeUrl {
    NSString *string = [self jw_urlEncode];
    NSURL *url = [NSURL URLWithString:string];
    return url;
}

/// URL解码
- (NSString *)jw_urlDecode {
    NSString *string = [self stringByRemovingPercentEncoding];
    return string;
}

/// base64编码
- (NSString *)jw_base64Encode {
    NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];
    if (!data) {
        return @"";
    }
    return [data base64EncodedStringWithOptions:0];
}

/// base64解码
- (NSString *)jw_base64Decode {
    NSData *data = [[NSData alloc] initWithBase64EncodedString:self options:(NSDataBase64DecodingIgnoreUnknownCharacters)];
    if (!data) {
        return @"";
    }
    return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}

/// int转16进制字符串
+ (NSString *)jw_hexStringWithInt:(NSUInteger)value {
    return [NSString stringWithFormat:@"%lX", (NSUInteger)value];
}

/// int转2进制字符串
+ (NSString *)jw_binaryStringWithInt:(NSInteger)value {
    NSMutableString *binaryString = [[NSMutableString alloc] init];
    while (value > 0) {
        [binaryString insertString:(value & 1 ? @"1" : @"0") atIndex:0];
        value >>= 1;
    }
    if ([binaryString length] == 0) {
        binaryString = [NSMutableString stringWithString:@"0"];
    }
    return binaryString;
}

/// 判断是否16进制字符串
- (BOOL)jw_isHexString {
    NSCharacterSet *hexCharaSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789ABCDEFabcdef"];
    NSString *value = [self stringByTrimmingCharactersInSet:hexCharaSet];
    value = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if (value.length > 0) {
        return NO;
    }
    return YES;
}

/// 16进制字符串转NSData
- (NSData *)jw_hexToData {
    if (![self jw_isHexString]) {
        return [NSData data];
    }
    NSString *rawStr = [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSUInteger length = rawStr.length;
    if (length == 0) {
        return [NSData data];
    }
    if (length % 2 != 0) {
        rawStr = [NSString stringWithFormat:@"0%@", rawStr];
        length++;
    }
    NSMutableData *data = [[NSMutableData alloc] init];
    for (NSUInteger i = 0; i < length; i += 2) {
        NSRange range = NSMakeRange(i, 2);
        NSString *pair = [rawStr substringWithRange:range];
        NSScanner *scanner = [NSScanner scannerWithString:pair];
        unsigned int decimalValue;
        [scanner scanHexInt:&decimalValue];
        [data appendBytes:&decimalValue length:1];
    }
    return data;
}

/// 16进制字符串转int
- (NSUInteger)jw_hexToInt {
    return (NSUInteger)strtoul([self UTF8String], 0, 16);
}

/// 16进制字符串转2进制字符串
- (NSString *)jw_hexToBinaryString {
    if (![self jw_isHexString]) {
        return @"";
    }
    NSString *rawStr = [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSUInteger length = rawStr.length;
    if (length == 0) {
        return self;
    }
    if (length % 2 != 0) {
        rawStr = [NSString stringWithFormat:@"0%@", rawStr];
        length++;
    }
    NSMutableString *valueString = [NSMutableString string];
    for (NSUInteger i = 0; i < length; i += 2) {
        NSRange range = NSMakeRange(i, 2);
        NSString *pair = [rawStr substringWithRange:range];
        NSScanner *scanner = [NSScanner scannerWithString:pair];
        unsigned int decimalValue;
        [scanner scanHexInt:&decimalValue];
        [valueString appendFormat:@"%08u", decimalValue];
    }
    return valueString;
}

/// 判断是否2进制字符串
- (BOOL)jw_isBinaryString {
    NSCharacterSet *hexCharaSet = [NSCharacterSet characterSetWithCharactersInString:@"01"];
    NSString *value = [self stringByTrimmingCharactersInSet:hexCharaSet];
    value = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if (value.length > 0) {
        return NO;
    }
    return YES;
}

/// 2进制字符串转int
- (NSUInteger)jw_binaryToInt {
    return (NSUInteger)strtoul([self UTF8String], 0, 2);
}

/// 2进制字符串转16进制字符串
- (NSString *)jw_binaryToHexString {
    if (![self jw_isBinaryString]) {
        return @"";
    }
    NSString *rawStr = [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSUInteger length = rawStr.length;
    if (length == 0) {
        return @"";
    }
    NSMutableString *valueString = [NSMutableString string];
    for (NSUInteger i = 0; i < length; i += 8) {
        NSRange range = NSMakeRange(i, 8);
        NSString *pair = [rawStr substringWithRange:range];
        unsigned long decimalValue = strtoul([pair UTF8String], 0, 2);
        [valueString appendFormat:@"%02lX", decimalValue];
    }
    return valueString;
}

/// ascii字符串转16进制字符串
- (NSString *)jw_asciiToHexString {
    NSMutableString *muString = [[NSMutableString alloc] init];
    const char *ch = [self cStringUsingEncoding:NSASCIIStringEncoding];
    for (int i = 0; i < strlen(ch); i++) {
        [muString appendString:[NSString stringWithFormat:@"%X",ch[i]]];
    }
    return muString;
}

// 转Int 4舍5入
- (NSInteger)jw_intRound {
    return lround([self doubleValue]);
}

// 转Int 下取整
- (NSInteger)jw_intFloor {
    return floor([self doubleValue]);
}

// 转Int 上取整
- (NSInteger)jw_intCeil {
    return ceil([self doubleValue]);
}




@end
